package com.xuelang.pipeline.client.common.util;

import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.vo.UserVO;
import org.slf4j.MDC;

/**
 * 用户上下文
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
public class UserContext {
  
    private static final ThreadLocal<UserVO> threadLocal = new ThreadLocal<>();
  
    public static void setCurrentUser(UserVO user) {
        threadLocal.set(user);  
    }  
  
    public static UserVO getCurrentUser() {
        return threadLocal.get();  
    }  
  
    public static void clearCurrentUser() {  
        threadLocal.remove();  
    }

    /**
     * 获取用户id
     */
    public static String getUserId() {
        try {
            String loginId = MDC.get(Constant.HIFORCE_USER_ID);
            if (org.apache.commons.lang3.StringUtils.isNotBlank(loginId)) {
                return loginId;
            } else {
                return "-1";
            }
        } catch (Exception e) {
            return "-1";
        }
    }


    /**
     * 获取用户名
     */
    public static String getUserName() {
        try {
            String loginId = MDC.get(Constant.HIFORCE_USER_ACCOUNT);
            if (org.apache.commons.lang3.StringUtils.isNotBlank(loginId)) {
                return loginId;
            } else {
                return "-1";
            }
        } catch (Exception e) {
            return "-1";
        }
    }
}